# tested with python 3.2, numpy 1.6.1, scipy 0.10.1

import math
import csv;
from numpy import linspace
from scipy import exp, array
from scipy.signal import firwin, lsim
from scipy.fftpack import fft, ifft
import genblepcommon

a500_unfiltered, a1200_unfiltered = genblepcommon.prepare_unfiltered()

def filter_signal(signal, amiga_model, audio_filter_mode):
    
    ts1, ts2 = genblepcommon.get_amiga_filter_ts(amiga_model, audio_filter_mode)
    t = linspace(0.0, 2047.0 / genblepcommon.AMIGA_CLOCK, 2048)
    
    if (ts1 == ( [1], [1] )):
        r = signal;
    else:
        r = lsim(ts1, signal, t)[1];
        
    if (ts2 != ( [1], [1] )):
        r = lsim(ts2, r, t)[1];
        
    return r;

def calc_model_500(model):

	print(model)

	amiga500_on = filter_signal(a500_unfiltered, model, 'led filter on')
	amiga500_off = filter_signal(a500_unfiltered, model, 'led filter off')

	genblepcommon.blep_calc('lsim-{0}'.format(model), 
							amiga500_off, amiga500_on, a500_unfiltered)

def calc_model_1200(model):

	print(model)

	amiga1200_on = filter_signal(a1200_unfiltered, model, 'led filter on')
	amiga1200_off = filter_signal(a1200_unfiltered, model, 'led filter off')

	genblepcommon.blep_calc('lsim-{0}'.format(model), 
							amiga1200_off, amiga1200_on, a1200_unfiltered)

def main():

	models500 = ["A500 R6a7", "A500", "A500+", "A600", "A2000 #1", "A2000 #2", "A3000", "CDTV", "A500 WinUAE"]
	models1200 = ["A1200", "A1200 #2", "CD32", "A4000", "A1200 WinUAE"]

	print("")
	print(genblepcommon.VIDEO_MODE)
	print("")

	for model in models500:
		calc_model_500(model)

	for model in models1200:
		calc_model_1200(model)

if __name__ == '__main__':

    genblepcommon.AMIGA_CLOCK = 3546895
    genblepcommon.VIDEO_MODE = 'PAL'

    main()

    genblepcommon.AMIGA_CLOCK = 3579545
    genblepcommon.VIDEO_MODE = 'NTSC'
    main()

